package com.sl.hx.shop.ui.home.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sl.hx.shop.R;
import com.sl.hx.shop.base.BaseActivity;
import com.sl.hx.shop.base.BasePresenter;
import com.sl.hx.shop.bean.HotSearch;
import com.sl.hx.shop.bean.SearchResult;
import com.sl.hx.shop.ui.classify.activity.SearchResultActivity;
import com.sl.hx.shop.ui.home.adapter.HotSearchAdapter;
import com.sl.hx.shop.ui.home.adapter.SearchHistoryAdapter;
import com.sl.hx.shop.ui.home.contacts.SearchContacts;
import com.sl.hx.shop.ui.home.presenter.SearchPresenter;
import com.sl.hx.shop.utils.SearchHistoryUtil;
import com.sl.hx.shop.widget.FlowLayoutManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import mtopsdk.common.util.StringUtils;

public class SearchActivity extends BaseActivity<SearchContacts.view, SearchPresenter> implements SearchContacts.view {

    @BindView(R.id.edittext)
    EditText edittext;
    @BindView(R.id.close)
    TextView close;
    @BindView(R.id.delete)
    ImageView delete;
    @BindView(R.id.history_search)
    RecyclerView mRvSearchHistory;
    @BindView(R.id.hot_search)
    RecyclerView hotSearch;
    @BindView(R.id.cleantext)
    ImageView cleantext;

    private String inputContent;

    private SearchHistoryAdapter mSearchHistoryAdapter; //历史记录adapter
    private HotSearchAdapter mHotSearchAdapter; //热门搜索adapter
    private List<HotSearch> hotSearches = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_search;
    }

    @Override
    protected SearchPresenter createPresenter() {
        return new SearchPresenter(mContext);
    }

    @Override
    protected void initView() {
        initRecycle();
        initEditText();
        mPresenter.getHotSearch();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.getAllSearchHistory();
    }

    @Override
    protected void initData() {

    }


    @OnClick({R.id.close, R.id.delete, R.id.cleantext})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.close:
                finish();
                break;
            //清空搜索历史
            case R.id.delete:
                mSearchHistoryAdapter.setNewData(new ArrayList<String>());
                mPresenter.deleteAllHistory();
                break;
            //清空输入的字符
            case R.id.cleantext:
                edittext.getText().clear();
                break;
        }
    }

    private void initRecycle() {
        //历史记录列表
        mSearchHistoryAdapter = new SearchHistoryAdapter(new ArrayList<String>());
        mRvSearchHistory.setLayoutManager(new FlowLayoutManager());
        mRvSearchHistory.setAdapter(mSearchHistoryAdapter);
        mSearchHistoryAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                SearchHistoryUtil.saveHistory(mSearchHistoryAdapter.getItem(position));
                //EventBus.getDefault().post(mSearchHistoryAdapter.getItem(position));
                Intent intent = new Intent(mContext, SearchResultActivity.class);
                intent.putExtra("searchTitle", mSearchHistoryAdapter.getItem(position));
                startActivity(intent);
            }
        });

        //热门搜索列表
        mHotSearchAdapter = new HotSearchAdapter(hotSearches);
        hotSearch.setLayoutManager(new FlowLayoutManager());
        hotSearch.setAdapter(mHotSearchAdapter);
        mHotSearchAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                SearchHistoryUtil.saveHistory(mHotSearchAdapter.getItem(position).getName());
                //EventBus.getDefault().post(mSearchHistoryAdapter.getItem(position));
                Intent intent = new Intent(mContext, SearchResultActivity.class);
                intent.putExtra("searchTitle", mHotSearchAdapter.getItem(position).getName());
                startActivity(intent);
            }
        });
    }


    @Override
    public void getAllSearchHistorySuccess(List<String> allSearchHistory) {
        mSearchHistoryAdapter.setNewData(allSearchHistory);

    }

    @Override
    public void ShowHotSearch(List<HotSearch> hotSearches) {
        mHotSearchAdapter.setNewData(hotSearches);
    }


    private void initEditText() {
        //输入状态监听
        edittext.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int count) {
                //输入文字中的状态
                if (count > 0) {
                    cleantext.setVisibility(View.VISIBLE);
                } else if (count == 0) {
                    cleantext.setVisibility(View.INVISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        //键盘回车键监听
        edittext.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
                    //先隐藏键盘
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);

                    //其次再做相应操作
                    inputContent = edittext.getText().toString();
                    if (StringUtils.isBlank(inputContent)) {
                    } else {
                        //保存搜索记录
                        SearchHistoryUtil.saveHistory(inputContent);
                        //跳转搜索结果页
                        intent();
                    }
                }
                return false;
            }
        });

    }

    private void intent() {
        Intent intent = new Intent(this, SearchResultActivity.class);
        intent.putExtra("searchTitle", edittext.getText().toString());
        startActivity(intent);
    }


}
